<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" import="java.util.*,java.sql.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>register_check</title>
</head>
<body>
<%!public static final String DBDRIVER = "com.mysql.jdbc.Driver";
	public static final String DBURL = "jdbc:mysql://localhost:3306/smm";
	public static final String DBUSER = "root";
	public static final String DBPASS = "657287";%>
	<%
	 String name=null;
	boolean flag=false;
	Connection conn=null;
	PreparedStatement stat=null;
	
	try{
		request.setCharacterEncoding("UTF-8");
		String password=request.getParameter("password");
		String password2=request.getParameter("password2");
		name=request.getParameter("username");
		if(password.equals(password2)){
			Class.forName(DBDRIVER);
			conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
			String sql="insert into user(username,password,gender,age,email,regdatetime)values(?,?,?,?,?,?)";
			stat=conn.prepareStatement(sql);
			stat.setString(1,name);
			stat.setString(2,request.getParameter("password"));
			stat.setString(3,request.getParameter("gender"));
			stat.setInt(4,Integer.parseInt(request.getParameter("age")));
			stat.setString(5,request.getParameter("email"));
			java.util.Date d = new java.util.Date();
			java.text.SimpleDateFormat dformat = new java.text.SimpleDateFormat("yyyy-MM-dd");
			String date = dformat.format(d);
			stat.setString(6,date);
			stat.executeUpdate();
			flag=true;
		}else{
			%>			
			<jsp:forward page="error.jsp">
				<jsp:param value="password isn't right!" name="p"/>
			</jsp:forward>
	<%
			}
		}catch(Exception e){
			 e.printStackTrace();
		}
		finally{    
	        if(null != stat)  
	            stat.close(); 
	        if(null != conn)  
	            conn.close();   
		}
		
		if(flag){
	%>
			<jsp:forward page="index.jsp">
				<jsp:param value="<%=name%>" name="user"/>
			</jsp:forward>
	<%
		}else{	
	%>
			<jsp:forward page="error.jsp">
				<jsp:param value="register error!" name="p"/>
			</jsp:forward>
	<%		
		}
	%>
</body>
</html>